//
//  MyEnshrineArticleController.m
//  tryshopping.boss
//
//  Created by Mr.Lee on 2016/12/23.
//  Copyright © 2016年 Reabam. All rights reserved.
//

#import "MyEnshrineArticleController.h"
#import "MyEnshrineViewController.h"
#import "MyEnshrineGoodsModel.h"
#import "MyEnshrineListCell.h"

@interface MyEnshrineArticleController () <MyEnshrineListCellDelegate, UIActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableV;

/** 模型数组 */
@property (nonatomic, strong) NSMutableArray *modelArray;

@end

@implementation MyEnshrineArticleController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = zuiqianhuiColor;
    
    
    
//    [self setUpUI];
//    // 请求数据
//    [self requestData];
//    
//    // 设置tableView
//    [self setUpTableView];
//    // 初始化模型
//    self.modelArray = [NSMutableArray array];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(test) name:@"BASEPage1CLICK" object:nil];
    
}
- (void)test{
    NSLog(@"-----");
    
    [self setUpUI];
    // 请求数据
    [self requestData];
    
    // 设置tableView
    [self setUpTableView];
    // 初始化模型
    self.modelArray = [NSMutableArray array];
}


- (void)setUpUI{
    // 是否全选
    [self.allSelectBtn setImage:[UIImage imageNamed:@"meixuanzhong_normal"] forState:UIControlStateNormal];
    [self.allSelectBtn setImage:[UIImage imageNamed:@"xuanzhong_normal"] forState:UIControlStateSelected];
    [self.allSelectBtn addTarget:self action:@selector(allSelectBtnClick1:) forControlEvents:UIControlEventTouchUpInside];
    self.allSelectBtn.selected = NO;
    
    // 全选
    self.allSelectLabel.text = @"全选";
    
    // 移除收藏夹
    [self.removeEnshrineBtn addTarget:self action:@selector(removeEnshrineBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.removeEnshrineBtn setBackgroundColor:COLOR(170, 170, 170)];
    self.removeEnshrineBtn.enabled = NO;
    
    
}

// 全选
- (void)allSelectBtnClick1:(UIButton *)sender{
    sender.selected = !sender.selected;
    
    if (sender.selected) {
        for (MyEnshrineGoodsModel *model in self.modelArray) {
            model.isSelected = YES;
        }
        
    } else{
        for (MyEnshrineGoodsModel *model in self.modelArray) {
            model.isSelected = NO;
        }
        
    }
    
    
    // 设置移除按钮
    NSInteger count = 0;
    for (MyEnshrineGoodsModel *model in self.modelArray) {
        if (model.isSelected == YES) {
            count = count + 1;
        }
    }
    if (count >0) {
        [self.removeEnshrineBtn setBackgroundColor:CommonColor];
        self.removeEnshrineBtn.enabled = YES;
    } else{
        [self.removeEnshrineBtn setBackgroundColor:COLOR(170, 170, 170)];
        self.removeEnshrineBtn.enabled = NO;
    }
    
    [self.tableV reloadData];
}


//单选
- (void)selectedBtnClick:(UIButton *)sender{
    MyEnshrineGoodsModel *model = self.modelArray[sender.tag];
    if (sender.selected) {
        model.isSelected = YES;
        
        NSInteger count = 0;
        for (MyEnshrineGoodsModel *model in self.modelArray) {
            if (model.isSelected == YES) {
                count = count + 1;
            }
        }
        if (count == self.modelArray.count) {
            self.allSelectBtn.selected = YES;
        } else{
            self.allSelectBtn.selected = NO;
            count = 0;
        }
        
        
    } else {
        model.isSelected = NO;
        self.allSelectBtn.selected = NO;
        
    }
    
    
    
    // 设置移除按钮
    NSInteger count = 0;
    for (MyEnshrineGoodsModel *model in self.modelArray) {
        if (model.isSelected == YES) {
            count = count + 1;
        }
    }
    if (count >0) {
        [self.removeEnshrineBtn setBackgroundColor:CommonColor];
        self.removeEnshrineBtn.enabled = YES;
    } else{
        [self.removeEnshrineBtn setBackgroundColor:COLOR(170, 170, 170)];
        self.removeEnshrineBtn.enabled = NO;
    }
}
//点击移除按钮
- (void)removeEnshrineBtnClick{
    NSLog(@"------");
    
    NSInteger count = 0;
    for (MyEnshrineGoodsModel *model in self.modelArray) {
        if (model.isSelected == YES) {
            count = count + 1;
        }
    }
    
    NSString *fomartSre = [NSString stringWithFormat:@"确定删除这%ld项", count];
    
    UIActionSheet *choiceSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:fomartSre, nil];
    [choiceSheet showInView:self.view];
    
    
}





#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        NSLog(@"00001111");
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Token1 = [user objectForKey:@"tokenId"];
        [params setObject:Token1 forKey:@"tokenId"];
        
        
        NSMutableArray *mArray = [NSMutableArray array];
        for (MyEnshrineGoodsModel *model in self.modelArray) {
            if (model.isSelected == YES) {
                [mArray addObject:model.id];
            }
        }
        [params setObject:mArray forKey:@"esIds"];
        
        
        NSLog(@"params&&&&^^==%@",params);
        //    [MBProgressHUD showIndeterminateHud:@"" delay:30 view:self.view];
        [HttpRequest requestData:@"User/Enshrine/Delete" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                
//                NSLog(@"EnshrineDelete******===%@",severDataDict.dataDict);
//                plistFileName(@"EnshrineDelete");
                
                [self.modelArray removeAllObjects];
                
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [MBProgressHUD showTextHud:requestResultStr view:self.view];
                    
                    [self requestData];
                    
                }
                else
                {
                    [MBProgressHUD hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [MBProgressHUD showTextHudSub:requestResultStr view:self.view];
                    
                }
                
            }else
            {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self.view];
            }
        }];
        
        
    } else if (buttonIndex == 1) { }
}


- (void)setUpTableView{
    self.tableV.separatorStyle= UITableViewCellSeparatorStyleNone;
    self.tableV.backgroundColor = zuiqianhuiColor;
}

- (void)requestData{
    
    NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token1 = [user objectForKey:@"tokenId"];
    [params setObject:Token1 forKey:@"tokenId"];
    [params setObject:@"MItem" forKey:@"esType"];
    
    
    NSLog(@"params&&&&^^==%@",params);
    [MBProgressHUD showIndeterminateHud:@"" delay:30 view:self.view];
    [HttpRequest requestData:@"User/Enshrine/List" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
//            NSLog(@"EnshrineList******===%@",severDataDict.dataDict);
//            plistFileName(@"EnshrineList");
//            
            [self.modelArray removeAllObjects];
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                NSMutableDictionary *mArray = [[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"itemList"];
                for (NSMutableDictionary *dicc  in mArray) {
                    MyEnshrineGoodsModel *model=[MyEnshrineGoodsModel mj_objectWithKeyValues:dicc];
                    [self.modelArray addObject:model];
                }
                
                [self.tableV reloadData];
                
                // 设置移除按钮
                NSInteger count = 0;
                for (MyEnshrineGoodsModel *model in self.modelArray) {
                    if (model.isSelected == YES) {
                        count = count + 1;
                    }
                }
                if (count >0) {
                    [self.removeEnshrineBtn setBackgroundColor:CommonColor];
                    self.removeEnshrineBtn.enabled = YES;
                } else{
                    [self.removeEnshrineBtn setBackgroundColor:COLOR(170, 170, 170)];
                    self.removeEnshrineBtn.enabled = NO;
                }
                
                // 设置全选按钮不可用
                if (self.modelArray.count ==0) {
                    self.allSelectBtn.selected = NO;
                    self.allSelectBtn.enabled = NO;
                }
            
            }
            else
            {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:requestResultStr view:self.view];
                
            }
            
        }else
        {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self.view];
        }
    }];
    
}


//每行的高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  120;
}
#pragma mark - TableView DateSource
// 返回多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
// 每组多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}
// 返回每行cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MyEnshrineGoodsModel *model = self.modelArray[indexPath.row];
    static NSString *CellIdentifier = @"MyEnshrineListCell";
    MyEnshrineListCell *cell = ( MyEnshrineListCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"MyEnshrineListCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    cell.index = indexPath.row;
    cell.model =model;
    return cell;
    
}



#pragma mark - TableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

@end
